package p.ithorns.sample;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;

import javax.annotation.Resource;
import javax.servlet.MultipartConfigElement;

/**
 * ModuleBConfig
 *
 * @author HtL
 * @date 2024/7/18 16:14
 * @since 1.0.0
 */
@Configuration
public class ModuleBConfig  {

    public static final String PATH = "/mb";

    private final static String MODULE_NAME = "ModuleB";


    public String getModuleName() {
        return MODULE_NAME;
    }

    public String getPath() {
        return PATH + "/*";
    }

    @Resource
    private MultipartConfigElement multipartConfig;

    // 不需要单独定义了，统一在ServletRegistrationRegistrar中注入
    @Bean(MODULE_NAME)
    public ServletRegistrationBean<DispatcherServlet> registerServlet(
            // 直接使用现成的dispatcherServlet
            @Qualifier( "dispatcherServlet") DispatcherServlet dispatcherServlet
    ) {
        String path = PATH + "/*";
        // AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        // DispatcherServlet servlet = new DispatcherServlet(context);
        ServletRegistrationBean<DispatcherServlet> bean = new ServletRegistrationBean<>(dispatcherServlet, path);
        bean.setName(MODULE_NAME);
        if (null != multipartConfig) {
            bean.setMultipartConfig(multipartConfig);
        }
        return bean;
    }


}